package com.rp.system.common.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rp.common.aspect.annotetion.AutoLog;
import com.rp.common.aspect.annotetion.ResponseResultBody;
import com.rp.common.result.ResultDTO;
import com.rp.common.utils.Selectway;
import com.rp.system.common.entity.Location;
import com.rp.system.common.entity.TypeLibrary;
import com.rp.system.common.service.TypeLibraryService;
import com.rp.system.common.service.impl.LocationServiceImpl;
import com.rp.system.common.service.impl.TypeLibraryServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author vicente
 * @since 2021-03-20
 */
@RestController
@Api(tags = "类型管理")
@ResponseResultBody
@RequestMapping("/type-library")
public class TypeLibraryController {
    
    @Autowired
    private TypeLibraryService typeLibraryService;

    @Autowired
    private Selectway selectway;

    @AutoLog(value = "类型管理-分页列表")
    @GetMapping("/list")
    @ApiOperation(value = "分页列表")
    @RequiresPermissions("typeLibrary:list")
    public ResultDTO<IPage<TypeLibrary>> list(
            @RequestParam(defaultValue = "1",required = false) @ApiParam(value = "当前页面") int pageNum,
            @RequestParam(defaultValue = "20",required = false) @ApiParam(value = "每页显示的数量") int pageSize,
            @RequestParam @ApiParam(value = "查询级别",required = true) Integer rank,
            @RequestParam(required = false) @ApiParam(value = "模糊查询字符串") String string){
        QueryWrapper<TypeLibrary> queryWrapper = new QueryWrapper<TypeLibrary>();
        selectway.fields(queryWrapper,string,"typeLibrary",null,null);
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("length(tl_code)", rank * (TypeLibraryServiceImpl.BUILD_MAX_SIZE + 1));
        return ResultDTO.success().data(typeLibraryService.page(new Page<>(pageNum, pageSize),queryWrapper));
    }

    @AutoLog(value = "类型管理-增加")
    @PostMapping("/insert")
    @ApiOperation(value = "增加")
    @RequiresPermissions("typeLibrary:update")
    public ResultDTO insert(@RequestBody TypeLibrary typeLibrary){
        return ResultDTO.tf(typeLibraryService.save(typeLibrary));
    }

    @AutoLog(value = "类型管理-修改")
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    @RequiresPermissions("typeLibrary:update")
    public ResultDTO update(@RequestBody TypeLibrary typeLibrary){
        return ResultDTO.tf(typeLibraryService.updateById(typeLibrary));
    }

    @AutoLog(value = "类型管理-删除")
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
    @RequiresPermissions("typeLibrary:update")
    public ResultDTO delete(@RequestBody @ApiParam(value = "id", required = true) String id){
        return ResultDTO.tf(typeLibraryService.removeById(id));
    }

    @AutoLog(value = "类型管理-批量删除")
    @DeleteMapping("/deleteAll")
    @ApiOperation(value = "批量删除")
    @RequiresPermissions("typeLibrary:update")
    public ResultDTO deleteAll(@RequestBody @ApiParam(value = "id(英文逗号隔开)", required = true) String ids){
        return ResultDTO.tf(typeLibraryService.removeByIds(Arrays.asList(ids.split(","))));
    }

    @AutoLog(value = "类型管理-不分页列表")
    @GetMapping("/listAll")
    @ApiOperation(value = "不分页列表")
    @RequiresPermissions("typeLibrary:list")
    public ResultDTO<List<TypeLibrary>> listAll(@RequestParam @ApiParam(value = "查询级别",required = true) Integer rank){
        QueryWrapper<TypeLibrary> queryWrapper = new QueryWrapper<TypeLibrary>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("length(tl_code)", rank * (TypeLibraryServiceImpl.BUILD_MAX_SIZE + 1));
        return ResultDTO.success().data(typeLibraryService.list(queryWrapper));
    }

    @AutoLog(value = "类型管理-根据id查询子集")
    @GetMapping("/findSonById")
    @ApiOperation(value = "根据id查询子集")
    @RequiresPermissions("typeLibrary:list")
    public ResultDTO<List<TypeLibrary>> findSonById(@RequestParam @ApiParam(value = "主键ID",required = true) String id){
        QueryWrapper<TypeLibrary> queryWrapper = new QueryWrapper<TypeLibrary>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("parent_id", id);
        return ResultDTO.success().data(typeLibraryService.list(queryWrapper));
    }

    @AutoLog(value = "类型管理-根据code查询子集")
    @GetMapping("/findSonByTlCode")
    @ApiOperation(value = "根据code查询子集")
    @RequiresPermissions("location:list")
    public ResultDTO<List<TypeLibrary>> findSonByLocCode(@RequestParam @ApiParam(value = "主键ID",required = true) String tlCode){
        QueryWrapper<TypeLibrary> queryWrapper = new QueryWrapper<TypeLibrary>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.likeRight("tl_code", tlCode);
        queryWrapper.eq("length(tl_code)", tlCode.length() + LocationServiceImpl.BUILD_MAX_SIZE +1);
        return ResultDTO.success().data(typeLibraryService.list(queryWrapper));
    }
}
